#ifndef _SHAPE_FUNC_H_
#define _SHAPE_FUNC_H_

#include "shape.h"
#include "quadratic_math.h"


#include "shape.h"
#include "mc.h"

#include "cl_scene.h"
#include "triangle_mesh_ocl.h"

INLINE float shape_area(shape_info_t shape_info,cl_scene_info_t scene_info)
{
	triangle_t triangle;
	load_triangle_points(scene_info.shape_data,shape_info,&triangle);
	return triangle_area(&triangle);
}

INLINE void shape_sample_on_shape(shape_info_t shape_info,cl_scene_info_t scene_info,float u0,float u1,normal3f_t *ns,point3f_t *pr)
{
	triangle_t triangle;
	load_triangle_points(scene_info.shape_data,shape_info,&triangle);
	triangle_sample(&triangle,u0,u1,ns,pr);
}
INLINE void shape_sample(GLOBAL shape_info_t* shape_info,cl_scene_info_t scene_info,const point3f_t* p,float u0,float u1,normal3f_t *ns,point3f_t *pr)
{
	triangle_t triangle;
	load_triangle_points(scene_info.shape_data,*shape_info,&triangle);
	triangle_sample(&triangle,u0,u1,ns,pr);
}
#include "primitive.h"
INLINE float shape_pdf(shape_info_t shape_info,cl_scene_info_t scene_info,const point3f_t* p,const vector3f_t* wi)
{
	differential_geometry_t dg_light;
	ray_t ray;
	rinit(ray,*p,*wi);
	float thit,epsilon;
	triangle_t triangle;
	GLOBAL const float* triangle_mesh_start = load_triangle_points(scene_info.shape_data,shape_info,&triangle);
	if(!intersect_triangle(triangle_mesh_start,shape_info.shape_type, &triangle,&ray,&thit,&dg_light,&epsilon))
	{return 0.f;}

	vector3f_t v_tmp;
	vsub(v_tmp,dg_light.p,*p);
	float dsqr = vdot(v_tmp,v_tmp);
	float wod = fabs(vdot(dg_light.nn,*wi));
	float down = (wod * triangle_area(&triangle));
	if (down == 0.f) return 0.f;
	return dsqr / down;
}
#endif
